<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/25 0025
 * Time: 10:26
 */
namespace app\index\controller;
//use http\Env\Request;
use app\index\model\prizes;
use think\Db;
use think\Request;

class Page
{
    public function page_pic(){
        $page = input('page',2);
        $limit = input('limit',10);
        $data = Db::name('')->limit(($page-1)*$limit,$limit)->select();
        return json(['code'=>200,'data'=>$data]);
    }

    /**兑换奖品分页
     * @param Request $request
     * @return mixed
     */
    public function getPrizes(Request $request){
        $p = new prizes();
        $data = $p->getPrizes($request);
        return $data[1];
    }

    /**兑换奖品记录分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getRecord(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $data = Db::name('record')->order('time desc')->limit(($page-1)*$limit,$limit)->select();
        return $data;
    }

    /**用户分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function luck(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $rresd = Db::connect();
        $data = $rresd->name('p_management')->order('total_integral desc')->limit(($page-1)*$limit,$limit)->select();
        foreach ($data as $key=>$value){
            if($value['friend']){
                $value['friend'] = json_decode($value['friend'],true);
                $arr = $rresd->name('p_management')->field('nickname')->where('openid','in',$value['friend'])->select();
                $arrs = [];
                foreach ($arr as $values){
                    $arrs[] = $values['nickname'];
                }
                $data[$key]['friend'] = implode(',',$arrs);
            }
        }
        return $data;
    }

    /**题目分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function topic(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $data = Db::name('topic_management')->limit(($page-1)*$limit,$limit)->select();
        foreach ($data as $key=>$value){
            if($value['optional']){
                $data[$key]['optional']=implode(';',json_decode($value['optional'],true));
            }
            //对答案的数据转化
            if($value['answer']==0){
                $data[$key]['answer']='A';
            }elseif($value['answer']==1){
                $data[$key]['answer']='B';
            }elseif($value['answer']==2){
                $data[$key]['answer']='C';
            }elseif($value['answer']==3){
                $data[$key]['answer']='D';
            }
        }
        return $data;
    }
}